/*
 * @lc app=leetcode.cn id=347 lang=cpp
 *
 * [347] 前 K 个高频元素
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    typedef pair<int, int> PAIR;
    struct MyComp
    {
        bool operator()(const PAIR& lhs, const PAIR& rhs)
        {
            return lhs.second > rhs.second;
        }
    };
    
    vector<int> topKFrequent(vector<int>& nums, int k) {
        map<int, int> m;
        for(auto n : nums)
            m[n]++;
        
        vector<PAIR> v;
        for(auto& it : m)
        {
            v.push_back(it);
        }
        
        sort(v.begin(), v.end(), MyComp());
        
        vector<int> ans;
        for(int i = 0; i < k; i++)
        {
            ans.push_back(v[i].first);

        }
        return ans;
    }
};
// @lc code=end

